package com.example.demo.controller;


import com.example.demo.dto.PaginationDto;
import com.example.demo.dto.QuerySysUserDto;
import com.example.demo.dto.StatusDto;
import com.example.demo.dto.SysUserDto;
import com.example.demo.model.Result;
import com.example.demo.service.SysUserService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/sys-user")
@AllArgsConstructor
public class SysUserController {

    private SysUserService sysUserService;


    @ApiOperation("获取用户分页数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "查询的用户名"),
            @ApiImplicitParam(name = "mobile", value = "查询电话"),
            @ApiImplicitParam(name = "email", value = "查询邮箱"),
            @ApiImplicitParam(name = "current", value = "第几页数据", required = true),
            @ApiImplicitParam(name = "size", value = "每页的数据")
    })
    @GetMapping
    public Result getPageData(QuerySysUserDto querySysUserDto, PaginationDto paginationDto) {
        return sysUserService.getPageData(querySysUserDto, paginationDto);
    }

    @ApiOperation("根据用户id获取用户信息，用于切换状态的使用")
    @GetMapping("/{id}")
    public Result getById(@PathVariable("id") Integer id) {
        return sysUserService.getById(id);
    }

    /**
     * http://localhost:8080/sys-user/23
     * 对于前端网络请求框架 axios 来说，除了GET，其他所有的请求(PUT、DELETE、POST)他们的MIME都是 application/json
     * 所以，我们在后端接受前端的请求参数参数的时候一定要使用 @RequestBody； 既然用了 @RequestBody 那么就必须要封装对象了
     */
    @PutMapping("/{id}")
    public Result changeStatus(@PathVariable("id") Integer id, @RequestBody StatusDto statusDto) {
        statusDto.setId(id);
        return sysUserService.changeStatus(statusDto);
    }

    @PostMapping
    public Result addSysUser(@RequestBody SysUserDto sysUserDto) {
        return sysUserService.addSysUser(sysUserDto);
    }

    @ApiOperation("根据用户id获取用户信息，用于编辑用户的时候使用")
    @GetMapping("/e/{id}")
    public Result getSysUserForEdit(@PathVariable("id") Integer id) {
        return sysUserService.getByIdForEdit(id);
    }

    @PutMapping("/e/{id}")
    public Result edit(@PathVariable("id") Integer id, @RequestBody SysUserDto sysUserDto) {
        return sysUserService.edit(sysUserDto);
    }
    @GetMapping("/d/{id}")
    public Result delById(@PathVariable("id") Integer id) {
        if(sysUserService.delById(id)){
            return Result.buildSuccess();
        }
        return Result.buildSuccess("删除失败",-1);
    }
}
